package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.DeviceOnlineOfflineRecordService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DeviceOnlineOfflineRecordDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LinYinDong
 * @date 2024/5/22 10:55
 */
@RestController
@RequestMapping("/api/deviceOnlineOfflineRecord")
public class DeviceOnlineOfflineRecordController extends UserInfoController {
    @Resource
    private DeviceOnlineOfflineRecordService deviceOnlineOfflineRecordService;
    @PostMapping("/deviceOnlineOfflineList")
    public ServerResponse deviceOnlineOfflineList(@RequestBody DeviceOnlineOfflineRecordDto deviceOnlineOfflineRecordDto, HttpServletRequest request) {
        UserInfo userInfo = super.getLoginUserInfo(request);
        if(StringUtils.isEmpty(deviceOnlineOfflineRecordDto.getDeviceNo())){
            return ServerResponse.createByErrorMessage("设备号不能为空");
        }
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            deviceOnlineOfflineRecordDto.setUserId(userInfo.getParentId());
        } else {
            deviceOnlineOfflineRecordDto.setUserId(userInfo.getId());
        }
        deviceOnlineOfflineRecordDto.setUserInfo(userInfo);

        return ServerResponse.createBySuccess(deviceOnlineOfflineRecordService.deviceOnlineOfflineByDeviceNoList(deviceOnlineOfflineRecordDto));
    }
}
